package com.qianyu.core.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket backApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("后台管理")
                .apiInfo(apiInfo())
                .host("127.0.0.1:8089")
                .globalOperationParameters(new ArrayList<Parameter>() {
                    private static final long serialVersionUID = 1L;

                    {
                        add(new ParameterBuilder().name("Authorization")
                                .description("请求可能需要在HTTP header中加入token。\r\n请填写\"Bearer {Authorization}\"")
                                .modelRef(new ModelRef("string"))
                                .parameterType("header").required(false).build());
                    }
                }).select()
                .apis(RequestHandlerSelectors.basePackage("com.qianyu.api.controller.back")).paths(PathSelectors.any())
                .build();
    }


    @Bean
    public Docket frontApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("前台管理")
                .apiInfo(apiInfo())
                .host("127.0.0.1:8089")
                .globalOperationParameters(new ArrayList<Parameter>() {
                    private static final long serialVersionUID = 1L;

                    {
                        add(new ParameterBuilder().name("Authorization")
                                .description("请求可能需要在HTTP header中加入token。\r\n请填写\"Bearer {Authorization}\"")
                                .modelRef(new ModelRef("string"))
                                .parameterType("header").required(false).build());
                    }
                }).select()
                .apis(RequestHandlerSelectors.basePackage("com.qianyu.api.controller.front")).paths(PathSelectors.any())
                .build();
    }



    @Bean
    public Docket commonApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("公共管理")
                .apiInfo(apiInfo())
                .host("127.0.0.1:8089")
                .globalOperationParameters(new ArrayList<Parameter>() {
                    private static final long serialVersionUID = 1L;

                    {
                        add(new ParameterBuilder().name("Authorization")
                                .description("请求可能需要在HTTP header中加入token。\r\n请填写\"Bearer {Authorization}\"")
                                .modelRef(new ModelRef("string"))
                                .parameterType("header").required(false).build());
                    }
                }).select()
                .apis(RequestHandlerSelectors.basePackage("com.qianyu.api.controller.comm")).paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("RESTful APIs").description("api文档结构")
                .termsOfServiceUrl("http://www.cd120.info/").contact(new Contact("谦屿科技", "", "")).title("小飞棍").version("1.0").build();
    }
}
